package com.chou.sts.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.chou.sts.domain.Student;
import com.chou.sts.service.StudentService;

@Controller
@RequestMapping("/api")
public class StudentController 
{
	private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

	@Autowired
	private StudentService studentService;
	
	@RequestMapping(value="student/{id}", method=RequestMethod.GET, headers="Accept=application/xml, application/json")
	@ResponseBody
	public Student get(@PathVariable int id, Model model)
	{
		logger.info("Getting student by id");
		Student student = studentService.get(id);		
		//model.addAttribute("student", student);
		return student;
	}
	
	@RequestMapping(value="students", method=RequestMethod.GET)
	@ResponseBody
	public List<Student> getAll(Model model)
	{
		logger.info("Get all students");		
		List<Student> students = studentService.getAll();
		//model.addAttribute("students", students);
		return students;
	}
	
	@RequestMapping(value="student/update", method=RequestMethod.POST)
	public String update(@ModelAttribute("student") Student student, Model model)
	{
		logger.info("Update student");
		studentService.save(student);
		model.addAttribute("student", student);
		return "student";
	}
}
